﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace Xiaozilang_Ordering_System
{
    class Shopping_Cart
    {
        //购物车界面存储填充数据
        public static DataTable ds=null;
        //订餐界面存储填充数据
        public static DataTable dc = null;
        //订单是否存在判断
        public static DataTable dd = null,dl=null;
        public static string judgedd = null;
        //充值
        public static DataTable rr = null;
        //建立填充
        //public static SqlDataAdapter dap = new SqlDataAdapter();
        //传递客户ID
        public static string UserJudge;
        //历史订单数据显示判断
        public static bool HistoricalOrders;
        //存储数据库语句 , total:总金额 ,数量:numberSql
        public static string totalSql = "",numberSql="";
        //计算传递存储  ,number:结算时查询和订单删除，总金额：total ,number:计算数量
        public static string numberIns, total = "0", number="0";
       /*
        * 购物车界面显示内容
        */
        public static void CartSelect()
        {
            //连接数据库
            //DBHelper.InitConnection();
            totalSql = string .Format ("  select c.BSID, dName,unitPrice,quantity,image,b.dishID,c.OrderID,odID,e.BSname from User1 a, Dish b, Order1 c, OrderDetail d, Business e where  d.dishID = b.dishID  and d.number = 1 and c.userID = {0} and c.OrderID = d.orderID  and c.BSID = e.BSID and a.userID = c.userID", UserJudge);
            ds = DBHelper.SelectByAdapter(totalSql);
            //填充数据
            //dap.Fill(ds);
        }

        //删除按钮
        public static void DeleteButton()
        {
            totalSql = string.Format("delete from OrderDetail where odID=(select top 1 d.odID from User1 a, Dish b, Order1 c, OrderDetail d, Business e where e.BSID = b.BSID and d.dishID = b.dishID and d.orderID = c.OrderID and c.userID = a.userID and a.userID = {0} and d.number = 1  and d.odID={1}) ", UserJudge, numberIns);
            DBHelper.GetNonQuery(totalSql);
        }
        /*
        * 订单界面显示内容
        */
        public static void OrderPage()
        {
            //DBHelper.InitConnection();
            //历史订单显示
            if (HistoricalOrders==false)
            {
                totalSql = string .Format ("select a.USaddress 客户地址,e.BSaddress 商家地址, amount,b.dName ,c.OrderID,e.BSname,c.deliveryTime,c.orderTime,orderStatus,b.unitPrice,d.odID ,e.BSID from User1 a,Dish b, Order1 c,OrderDetail d, Business e where e.BSID = b.BSID and d.dishID = b.dishID and d.orderID = c.OrderID and c.userID = a.userID  and d.number=0 and orderStatus=1  and a.userID='{0}'", UserJudge);
            }
            else
            {
                totalSql = string .Format ("select a.USaddress 客户地址,e.BSaddress 商家地址, amount,b.dName ,c.OrderID,e.BSname,c.deliveryTime,c.orderTime,orderStatus,b.unitPrice,d.odID ,e.BSID from User1 a,Dish b, Order1 c,OrderDetail d, Business e where e.BSID = b.BSID and d.dishID = b.dishID and d.orderID = c.OrderID and c.userID = a.userID and orderStatus=0  and d.number=0  and a.userID='{0}'", UserJudge);
            }
            dc = DBHelper.SelectByAdapter(totalSql);
            dc.Columns.Add("orderStatusDtata",typeof(string ));
            
            for (int i=0;i<dc.Rows.Count;i++)
            {
                string psz = dc.Rows[i]["orderStatus"].ToString();
                if (psz == "0"  ||  psz == "")
                {
                    dc.Rows[i]["orderStatusDtata"] = "派送中";
                }
                else
                {
                    dc.Rows[i]["orderStatusDtata"] = "已送达";
                }
            }
        }
        //确定收到
        public static void ConfirmReceipt()
        {
            //历史订单
            totalSql = string.Format("  update OrderDetail set orderStatus=1  where  orderID = (select c.OrderID from User1 a, Dish b, Order1 c, OrderDetail d, Business e where e.BSID = b.BSID and d.dishID = b.dishID and d.orderID = c.OrderID and c.userID = a.userID and a.userID = {0} and d.number = 0 and d.orderStatus = 0 and d.odID = {1}) ", UserJudge, numberIns);
            DBHelper.GetNonQuery(totalSql);
            //确认收到的时间
            string time = string.Format("update Order1 set deliveryTime='{0}' where  orderID=(select c.OrderID from User1 a, Dish b, Order1 c, OrderDetail d, Business e where e.BSID = b.BSID and d.dishID = b.dishID and d.orderID = c.OrderID and c.userID = a.userID and a.userID = {1} and d.number = 0 and d.orderStatus = 1 and d.odID = {2}) ", DateTime.Now.ToString(), UserJudge, numberIns);
            DBHelper.GetNonQuery(time);
        }
        /*
         * 订单判断生成
         */
        public static void orderGenerationJugde()
        {
            totalSql = string.Format("select top 1 odID,a.orderID from OrderDetail a,Order1 b where a.orderID=(select top 1 a.OrderID from Order1 a,OrderDetail b where BSID={0} and a.userID={1} and a.OrderID=b.orderID and b.number=1) and a.userID={2} and number=1 and b.OrderID=a.orderID and b.BSID={3}", client.bsid,UserJudge, UserJudge, client.bsid);
            dd = DBHelper.SelectByAdapter(totalSql);
        }
        /*
         * 订单生成
         */
        public static void orderGeneration()
        {
            string dati = DateTime.Now.ToString();
            totalSql = string.Format("insert into Order1 (BSID,orderTime,userID,deliveryTime) values('{0}','{1}','{2}','{3}')", client.bsid, dati, UserJudge, DateTime.Now.AddMinutes(40));
            dd = DBHelper.SelectByAdapter(totalSql);
            totalSql = string.Format("select orderID from Order1 where orderTime='{0}'", dati);
            dl = DBHelper.SelectByAdapter(totalSql);
        }

        public static void rechargeRefresh()
        {
            totalSql = string.Format("select USMoney from User1 where userID='{0}'", UserJudge);
            rr = DBHelper.SelectByAdapter(totalSql);
        }
    }
}
